﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lottery.MediaData.Core {
    public static class DSHelper {
        /// <summary>
        /// 汇总媒体数据 表一
        /// </summary>
        /// <param name="allData">所有媒体数据310的预测场数</param>
        /// <returns>汇总数据，Key:媒体数， Value:场数</returns>
        public static Dictionary<int, int> ProcessAllData(List<DS> allData) {
            var result = new Dictionary<int, int>();
            foreach (var item in allData) {
                int three = item.Three;
                int one = item.One;
                int zero = item.Zero;

                // 三数一致
                if (three == one && three == zero) {
                    int a = three;
                    if (result.ContainsKey(a))
                        result[a] = ++result[a];
                    else
                        result.Add(a, 1);
                }

                // 两数一致(I)
                if (three == one && three != zero) {
                    int a = three;
                    int b = zero;

                    // 处理 a
                    if (result.ContainsKey(a))
                        result[a] = ++result[a];
                    else
                        result.Add(a, 1);
                    // 处理 b
                    if (result.ContainsKey(b))
                        result[b] = ++result[b];
                    else
                        result.Add(b, 1);
                }

                // 两数一致(II)
                if (three != one && three == zero) {
                    int a = three;
                    int b = one;

                    // 处理 a
                    if (result.ContainsKey(a))
                        result[a] = ++result[a];
                    else
                        result.Add(a, 1);
                    // 处理 b
                    if (result.ContainsKey(b))
                        result[b] = ++result[b];
                    else
                        result.Add(b, 1);
                }

                // 两数一致(III)
                if (three != one && one == zero) {
                    int a = three;
                    int b = one;
                    // 处理 a
                    if (result.ContainsKey(a))
                        result[a] = ++result[a];
                    else
                        result.Add(a, 1);
                    // 处理 b
                    if (result.ContainsKey(b))
                        result[b] = ++result[b];
                    else
                        result.Add(b, 1);
                }

                // 三数都不一样
                if (three != one && three != zero && one != zero) {
                    int a = three;
                    int b = one;
                    int c = zero;

                    // 处理 a
                    if (result.ContainsKey(a))
                        result[a] = ++result[a];
                    else
                        result.Add(a, 1);
                    // 处理 b
                    if (result.ContainsKey(b))
                        result[b] = ++result[b];
                    else
                        result.Add(b, 1);
                    // 处理 c
                    if (result.ContainsKey(c))
                        result[c] = ++result[c];
                    else
                        result.Add(c, 1);
                }
            }

            var d = from item in result
                    orderby item.Key descending
                    select item;
            result = new Dictionary<int, int>();
            foreach (var item in d) {
                result.Add(item.Key, item.Value);
            }
            return result;
        }

        private static List<string> ProcessFilteredList(List<DS> allData, List<FD> combineData) {
            var result = new List<string>();

            int index = 1;
            foreach (var item in combineData) {
                int mediaCount = item.MediaCount;
                StringBuilder line = new StringBuilder();
                line.Append(index++);
                line.Append("\t");

                foreach (var data in allData) {
                    StringBuilder sb = new StringBuilder();
                    if (data.Three == mediaCount)
                        sb.Append("3");

                    if (data.One == mediaCount )
                        sb.Append("1");
                    
                    if (data.Zero == mediaCount)
                        sb.Append("0");

                    line.Append(sb.ToString());
                    line.Append("\t");
                }

                line.Append(item.Low);
                line.Append("-");
                line.Append(item.Up);

                result.Add(line.ToString());
            }

            return result;
        }
    }
}
